﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Galaktika.BI.Extensibility;
using Galaktika.BI.Extensibility.Dom.Dac;
using Galaktika.BI.Runtime;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.Commands;
using Galaktika.BI.Silverlight.Services;

namespace Galaktika.BI.Silverlight.Controls
{
    public class DataActualiltyLabel : Label, IBindableObject, IEditableObject
    {
        public DataActualiltyLabel()
        {
            base.HorizontalAlignment = HorizontalAlignment.Center;
            base.VerticalAlignment = VerticalAlignment.Center;
        }

        public string AConnectionStringName  { get; set; }
        public string ACubeName { get; set; }
        public string AFormatString { get; set; }

        #region IEditableObject Members

        public void BeginEdit()
        {
        }

        public void CancelEdit()
        {
        }

        public void EndEdit()
        {
            var exe = this.Context.Services.GetService<IExecutionContext>();
            string connectionString = null;
            if (exe != null)
            {
                var conn = exe.GetNamedObject(this.Context, this.AConnectionStringName) as BrickConnection;
                if (conn != null)
                {
                    connectionString = exe.Parse(this.Context, conn.ConnectionString);
                }
            }

            if (!string.IsNullOrEmpty(connectionString))
            {
                var sessionId = this.GetSessionId(connectionString);
                var schema = CommandBuilder.CreateGetActualityDateSchema(
                    connectionString, 
                    sessionId, 
                    this.ACubeName);

                var userTaskMgr = this.Context.Services.GetService<IUserTaskManager>();
                if (userTaskMgr != null)
                {
                    var caption = string.Format(DataActualityLabelStrings.GettingActualityDate, base.Name);
                    userTaskMgr.RunTaskAsync(caption, state =>
                        {
                            using (var srv = new SyncDataService())
                            {
                                var res = srv.InvokeCommand(schema);
                                userTaskMgr.Dispatcher.BeginInvoke(() => 
                                    {
                                        var dateTime = DateTime.MinValue;
                                        if (DateTime.TryParse(
                                            res.Content,
                                            CultureInfo.InvariantCulture,
                                            DateTimeStyles.None,
                                            out dateTime))
                                        {
                                            if (string.IsNullOrEmpty(this.AFormatString))
                                            {
                                                base.Content = dateTime.ToString();
                                            }
                                            else
                                            {
                                                base.Content = dateTime.ToString(this.AFormatString);
                                            }
                                        }
                                    });
                            }
                        });
                }
            }
        }

        private string GetSessionId(string connectionString)
        {
            var sessionProvider = this.Context.Services.GetService<IAdomdSessionProvider>();
            if (sessionProvider != null)
            {
                return sessionProvider.GetSessionId(connectionString);
            }

            return string.Empty;
        }

        #endregion

        #region IBindableObject Members

        public IBindingContext Context { get; set; }

        private IDictionary<string, IBindingManager> m_Bindings;
        public IDictionary<string, IBindingManager> Bindings
        {
            get
            {
                if (m_Bindings == null)
                {
                    m_Bindings = new SafeDictionary<string, IBindingManager>();
                }

                return m_Bindings;
            }
        }

        #endregion
    }
}
